/*
 * Copyright 2011-2012 by The Regents of the University of California Licensed
 * under the Apache License, Version 2.0 (the "License"); you may not use this
 * file except in compliance with the License. you may obtain a copy of the
 * License from
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package edu.ucsd.db.bassarisk.query;

import edu.ucsd.db.bassarisk.query.bindingtree.BindingTreeElements.BindingTreeEdge;
import edu.ucsd.db.bassarisk.query.bindingtree.BindingTreeElements.BindingTreeElement;
import edu.ucsd.db.bassarisk.query.bindingtree.visitor.BindingTreeVisitor;

public class BindingTreeQuery {
  private BindingTreeEdge answerRootEdge;
  private BindingTreeEdge queryRootEdge;

  public BindingTreeQuery(final BindingTreeEdge queryRootEdge,
                          final BindingTreeEdge answerRootEdge) {
    this.queryRootEdge = queryRootEdge;
    this.answerRootEdge = answerRootEdge;
  }

  public <R extends BindingQueryTransformReturn, A> R
      applyTransformation(final BindingTreeVisitor<R, A> visitor, final A arg) {
    R retValue = this.queryRootEdge.accept(visitor, arg);
    this.queryRootEdge = (BindingTreeEdge) retValue.getTransformationRoot();
    this.answerRootEdge = retValue.getAnswerRoot();
    return retValue;
  }

  public <R, A> R applyVisitor(final BindingTreeVisitor<R, A> visitor, final A arg) {
    return this.queryRootEdge.accept(visitor, arg);
  }

  public BindingTreeEdge getAnswerRoot() {
    return this.answerRootEdge;
  }

  public BindingTreeEdge getQueryRoot() {
    return this.queryRootEdge;
  }

  public static interface BindingQueryTransformReturn extends
      TransformReturn<BindingTreeElement> {
    public BindingTreeEdge getAnswerRoot();
  }
}
